package com.xxx.controller;


import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.xxx.config.Request;
import com.xxx.pojo.*;
import com.xxx.pojo.vo.ClientVo;
import com.xxx.pojo.vo.OrdersVo;
import com.xxx.pojo.vo.ProductVo;
import com.xxx.service.OrdersService;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author George
 * @since 2023-07-17
 */
@RestController
@RequestMapping("/orders")
public class OrdersController {


    @Resource
    private OrdersService ordersService;


    /**
     * 连表查询订单
     *
     * @return 返回用户列表和总数
     */
    @ApiOperation(value = "根据条件查询订单列表", notes = "根据分页查询参数查询订单列表")
    @PostMapping("/ordersListByCondition")
    public Request getOrdersListByCondition(
            @RequestParam("searchValue") String searchValue,
            @RequestParam("pageNum") int pageNum,
            @RequestParam("pageSize") int pageSize) {

        Page<OrdersVo> mapPage = new Page<>(pageNum, pageSize, true);

        // 去除查询条件中的空格
        String finalSearchValue = StringUtils.isNotBlank(searchValue) ? searchValue.trim() : null;

        MPJLambdaWrapper<Orders> lambdaWrapper = new MPJLambdaWrapper<Orders>()
                .selectAll(Orders.class)
                .select(Product::getProductName,Product::getProductPicture,
                        Product::getProductPrice,Product::getShopName)
                .leftJoin(Product.class, Product::getProductId, Orders::getProductId);

        if (StringUtils.isNotBlank(finalSearchValue)) {
            lambdaWrapper.and(wrapper -> wrapper
//                    根据商品名称查询
                    .like(Product::getProductName, finalSearchValue)
                    .or()
//                    根据商品品牌查询
                    .like(Product::getProductBrand, finalSearchValue)
                    .or()
//                    根据商品品牌查询
                    .like(Orders::getOrdersId, finalSearchValue)
                    .or()
//                    根据产品类型查询
                    .like(Client::getClientId, finalSearchValue));
        }

        ordersService.selectJoinListPage(mapPage, OrdersVo.class,lambdaWrapper);
        List<OrdersVo> records = mapPage.getRecords();
        long total = mapPage.getTotal();

        return records.size() > 0 ? Request.suc(total,records) : Request.error("查询产品失败");
    }




    /**
     * 根据id查询订单详情
     *
     * @return 返回用户列表和总数
     */
    @ApiOperation(value = "根据订单编号查询订单详情", notes = "根据订单编号查询订单详情")
    @PostMapping("/ordersDetails")
    public Request getOrdersDetails(
            @RequestParam("ordersId") String ordersId) {
        List<OrdersVo> ordersVos = ordersService.selectJoinList(OrdersVo.class,
                new MPJLambdaWrapper<Orders>()
                        .selectAll(Orders.class)
                        .selectAll(Product.class)
                        .select(Client::getClientName)
                        .select(ClientDetail::getClientDetailAddress)
                        .leftJoin(Product.class, Product::getProductId, Orders::getProductId)
                        .leftJoin(Client.class,Client::getClientId,Orders::getClientId)
                        .leftJoin(ClientDetail.class,ClientDetail::getClientId,Orders::getClientId)
                        .eq(Orders::getOrdersId, ordersId)
        );
        return ordersVos.size() > 0 ? Request.suc(ordersVos) : Request.error("查询失败");
    }

}

